﻿CREATE PROCEDURE [steve].[spSteve_ApproveContent]
	@ContentId uniqueidentifier,
	@RevisionNumber int,
	@ApprovalMembershipId uniqueidentifier,
	@ApprovalDate datetime2(7)
AS
	declare @error int, @rc int

	update ContentRevision
	set ApprovalDate = @ApprovalDate
		, ApprovalMembershipId = @ApprovalMembershipId
	where ContentId = @ContentId
		and RevisionNumber = @RevisionNumber
		and ApprovalDate is null

	select @error = @@ERROR, @rc = @@ROWCOUNT
	if @error <> 0 return @error
	if @rc <> 1 
	begin
		raiserror ('Approval affected more than one piece of content, error occurred.', 16, 1)
		return 50000
	end

	update Content
	set LatestApprovedRevisionNumber = cr.MaxRevisionNumber
	from (
		select max(RevisionNumber) as MaxRevisionNumber
		from ContentRevision cr
		where cr.ContentId = @ContentId
			and ApprovalDate is not null) as cr
	where ContentId = @ContentId
	
	select @error = @@ERROR

RETURN @error